<template>
  <div class="approval" >
    <el-card class="box-card">
      <div class="head-title2">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class="info">
          <el-tooltip placement="left">
            <div slot="content">
              {{define}}
            </div>
            <div class="btns"></div>
          </el-tooltip>
        </div>
      </div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 选择时间 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('ApprReport.choiceTime')}}</span>
            <el-date-picker
              size="small"
              v-model="listQuery.startDate"
              type="month"
              value-format="yyyy-MM"
              :placeholder="$t('rule.beginDate')"
            ></el-date-picker>
            <b>{{$t('approval.go')}}</b>
            <el-date-picker
              size="small"
              v-model="listQuery.endDate"
              type="month"
              value-format="yyyy-MM"
              :placeholder="$t('rule.endDate')"
            ></el-date-picker>
          </div>
          <!-- APP渠道 -->
          <div class="searchItem">
            <span>{{$t('ElectricChannel.AppChannel')}}</span>
            <el-select v-model="listQuery.channel" :placeholder="$t('rule.choose')" size="small">
              <el-option
                v-for="item in appChannel"
                :key="item.channel"
                :label="item.org_name"
                :value="item.channel"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="menu-title-btn">
          <button type="button" @click="search">{{$t('btn.query')}}</button>
          <button type="button" @click="reset">{{$t('btn.reset')}}</button>
          <button
            type="button"
            @click="handleDownload"
            :disabled="expDis"
            v-if="custVolumeMonth_btn_export"
          >{{$t('btn.export')}}</button>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('processing.taskList')}}</div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        size="mini"
      >
        <!-- 时间 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="crtTime"
          width="160"
          :label="$t('approvalDetail.riskTime')"
        ></el-table-column>
        <!-- 客户注册量 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="registerCount"
          width="160"
          :label="$t('CustConversionTable.custRegisterVolume')"
        ></el-table-column>
        <!-- 新客户量 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="newCustomerCount"
          width="160"
          :label="$t('loanPlanReport.data3')"
        ></el-table-column>
        <!-- 续贷客户量 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="oldCustomerCount"
          width="180"
          :label="$t('loanPlanReport.data4')"
        ></el-table-column>
        <!-- 放款客户量 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="lentCustomerCount"
          width="180"
          :label="$t('CustConversionTable.loanVolume')"
        ></el-table-column>
        <!-- 放款金额 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="lentAmt"
          width="180"
          :label="$t('CustConversionTable.loanAmt')"
        ></el-table-column>
        <!-- 自动放款新客户数量 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="autoLentCount"
          width="180"
          :label="$t('CustConversionTable.AutoLoanNew')"
        ></el-table-column>
        <!-- 客户转化率 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="customerRate"
          width="180"
          :label="$t('CustConversionTable.custConversionRate')"
        ></el-table-column>
        <!-- 新客户占比 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="newCustomerRate"
          width="180"
          :label="$t('CustConversionTable.NewClientProp')"
        ></el-table-column>
        <!-- 新客户通过率 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="newCustomerPassRate"
          width="180"
          :label="$t('CustConversionTable.NewClientPassRate')"
        ></el-table-column>
        <!-- 续贷客户占比 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="oldCustomerRate"
          width="180"
          :label="$t('CustConversionTable.ContinuedLoanProp')"
        ></el-table-column>
        <!-- 续贷客户通过率 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="oldCustomerPassRate"
          width="180"
          :label="$t('CustConversionTable.ContinuedPassRate')"
        ></el-table-column>
        <!-- 自动放款新客户占比 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="autoLentRate"
          width="180"
          :label="$t('CustConversionTable.autoLoanNewProp')"
        ></el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getCustomerCount,
  getCustomerCountExport
} from "api/RiskManager/CustConversionTable/index";
import { getChannels } from "api/customQuery/index";
export default {
  data() {
    return {
      define: '',
      appChannel: [],
      custVolumeMonth_btn_export: false,
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 5,
        page: 1,
        startDate: "",
        endDate: "",
        channel: "",
        dateType: "2",
        queryType: "1"
      },
      tableData: [],
    };
  },
  computed: {
    ...mapGetters(["elements", "custVolumeMonthPage", 'getReportDefine'])
  },
  created() {
    if (this.custVolumeMonthPage.has(this.$route.path)) {
      this.listQuery = this.custVolumeMonthPage.get(this.$route.path);
    }
    // 渠道下拉
    getChannels().then(response => {
      this.appChannel = response.data;
      this.listQuery.channel = this.appChannel[0].channel;
      this.getList();
    });
    this.custVolumeMonth_btn_export = this.elements["custVolumeMonth:btn_export"];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'CUSTOMER_CONVERSION_TABLE_MONTH') {
        this.define = item.dictValue
      }
    })
  },
  methods: {
    GetDateStr(AddDayCount) {
      let dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      let y = dd.getFullYear();
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      // let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m;
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      if ((this.listQuery.startDate === this.GetDateStr(0) && this.listQuery.endDate === this.GetDateStr(0))
      || (this.listQuery.startDate === this.GetDateStr(0) && this.listQuery.endDate === '')
      || (this.listQuery.startDate === '' && this.listQuery.endDate === this.GetDateStr(0))
      || (this.listQuery.startDate === '' && this.listQuery.endDate === '')) {
        this.listQuery.queryType = '1';
      } else {
        this.listQuery.queryType = '2';
      }
      getCustomerCountExport(this.listQuery).then(response => {
        require.ensure([], () => {
          const { export_json_to_excel } = require("@/vendor/Export2Excel");
          const tHeader = [
            this.$t('approvalDetail.riskTime'),
            this.$t('CustConversionTable.custRegisterVolume'),
            this.$t('loanPlanReport.data3'),
            this.$t('loanPlanReport.data4'),
            this.$t('CustConversionTable.loanVolume'),
            this.$t('CustConversionTable.loanAmt'),
            this.$t('CustConversionTable.AutoLoanNew'),
            this.$t('CustConversionTable.custConversionRate'),
            this.$t('CustConversionTable.NewClientProp'),
            this.$t('CustConversionTable.NewClientPassRate'),
            this.$t('CustConversionTable.ContinuedLoanProp'),
            this.$t('CustConversionTable.ContinuedPassRate'),
            this.$t('CustConversionTable.autoLoanNewProp'),
          ];
          const filterVal = [
            "crtTime",
            "registerCount",
            "newCustomerCount",
            "oldCustomerCount",
            "lentCustomerCount",
            "lentAmt",
            "autoLentCount",
            "customerRate",
            "newCustomerRate",
            "newCustomerPassRate",
            "oldCustomerRate",
            "oldCustomerPassRate",
            "autoLentRate"
          ];
          const list = response.data;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, this.$t("CustConversionTable.CustVolumeMonthly"));
          this.listLoading = false;
        });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveCustVolumeMonthPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      if ((this.listQuery.startDate === this.GetDateStr(0) && this.listQuery.endDate === this.GetDateStr(0))
      || (this.listQuery.startDate === this.GetDateStr(0) && this.listQuery.endDate === '')
      || (this.listQuery.startDate === '' && this.listQuery.endDate === this.GetDateStr(0))
      || (this.listQuery.startDate === '' && this.listQuery.endDate === '')) {
        this.listQuery.queryType = '1';
      } else {
        this.listQuery.queryType = '2';
      }
      getCustomerCount(this.listQuery).then(response => {
        response.data.records.forEach((item, index) => {
          item.index =
            index + (this.listQuery.page - 1) * this.listQuery.limit + 1;
          let re = /(?=(?!\b)(\d{3})+$)/g;
          let str = String(item.lentAmt)
          item.lentAmt = str.replace(re, ',');
        });
        this.expDis = false;
        this.listLoading = false;
        this.tableData = response.data.records;
        this.total = Number(response.data.totalNumber);
      });
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.startDate = "";
      this.listQuery.endDate = "";
      this.listQuery.channel = this.appChannel[0].channel;
      this.listQuery.queryType = "1";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 200px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 200px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../../assets/images/question-mark.svg");
}
</style>